//服务器地址
// var isTest = true;
// var baseUrl = "http://119.39.65.55:20080/default/"; //测试库地址

//正式库地址
var isTest = false;
var baseUrl = "http://119.39.65.55:10056/default/"; //正式库地址

/* 语言包 */
var _lang = {
	domEmpty: '没有选中的dom元素',
	xhrError: '无法加载XMLHttpRequest模块！',
	ajaxTimeout: '请求超时',
	quit: '再按一次退出程序',
	netErr: '无法连接服务器,请检查网络'
};

//ajax调用
var apiManage = {
	//get方式获取,参数可用param方式
	get: function(url, param, success, err) {
		if (param != null) {
			url = apiManage.makeUrl(url, param);
		}
		DebugPrint(url);
		var sets = {
			url: url,
			type: 'GET',
			backType: 'JSON',
			success: success,
			error: err
		};
		this.ajax(sets);
	},

	//普通参数方式提交
	post: function(url, params, success, err) {
		var sets = {
			url: url,
			type: 'POST',
			backType: 'JSON',
			data: params,
			success: success,
			error: err,
			ContentType: 'application/x-www-form-urlencoded'
		};
		this.ajax(sets);
	},

	//json字符串参数方式提交
	postByJson: function(url, params, success, err) {
		var sets = {
			url: url,
			type: 'POST',
			backType: 'JSON',
			data: params,
			success: success,
			error: err,
			ContentType: "application/json"
		};
		this.ajax(sets);
	},

	ajax: function(sets) {
		var _self = this;
		if (!sets) {
			sets = {
				url: null
			};
		}
		if (!sets.url) {
			return;
		}
		var async = 'async' in sets ? sets.async : true;
		sets.type = 'type' in sets ? sets.type.toUpperCase() : 'GET';
		sets.backType = 'backType' in sets ? sets.backType.toUpperCase() : 'HTML';
		sets.beforeSend = 'beforeSend' in sets ? sets.beforeSend : null;
		sets.complete = 'complete' in sets ? sets.complete : null;
		sets.success = 'success' in sets ? sets.success : function() {};
		sets.error = 'error' in sets ? sets.error : function(e) {
			DebugPrint('ajax error : ' + JSON.stringify(e));
		}
		sets.ContentType = 'ContentType' in sets ? sets.ContentType : 'application/x-www-form-urlencoded';
		sets.header = 'header' in sets ? sets.header : false;

		//ios跨域
		//console.log("window.plus:" + window.plus);
		if (window.plus) {
			this.ajaxky(sets);
			return;		
		} 
		
		console.log("网页环境");
		var xhr = new window.XMLHttpRequest();
		xhr.open(sets.type, sets.url, async);
		if (typeof(xhr) == 'undefined') {
			if (sets.error) {
				sets.error(_lang.xhrError);
			}
			return;
		}
		if (sets.header) {
			for (var i = 0; i < sets.header.length; i++) {
				xhr.setRequestHeader(sets.header[i][0], sets.header[i][1]);
			}
		}
		///xhr.setRequestHeader("tokenid", "123456");
		//xhr.setRequestHeader("Access-Control-Allow-Origin","*");

		xhr.timeout = 'timeout' in sets ? sets.timeout : 0;
		if (sets.beforeSend) {
			sets.beforeSend();
		}
		var _apiManage = this;
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (sets.complete) {
					sets.complete();
				}
				if (xhr.status == 200) {
					if (sets.backType == 'HTML') {
						sets.success(xhr.responseText);
					} else if (sets.backType == 'JSON') {
						_apiManage.xhrSucess(sets, xhr);
					}
				}
			}
		}
		xhr.ontimeout = function() {
			sets.error(_lang.ajaxTimeout);
		}
		if (sets.error) {
			xhr.onerror = function(e) {
				sets.error(_lang.netErr);
				//sets.error(e);
			}
		}
		if (sets.type == 'POST') {
			var pd = '';
			if (sets.ContentType == 'application/x-www-form-urlencoded') {
				for (var k in sets.data) {
					pd += encodeURIComponent(k) + '=' + encodeURIComponent(sets.data[k]) + '&';
				}
				pd = pd.substr(0, pd.length - 1);
				//DebugPrint(pd);
				xhr.setRequestHeader('Content-Type', sets.ContentType);
				xhr.send(pd);
			} else {
				xhr.setRequestHeader('Content-Type', sets.ContentType);
				xhr.send(JSON.stringify(sets.data));
			}

		} else {
			xhr.send();
		}
	},


	ajaxky: function(sets) {
		if(!window.plus){
			return;
		}
		
		var xhr = new plus.net.XMLHttpRequest();

		var _apiManage = this;
		xhr.onreadystatechange = function() {
			switch (xhr.readyState) {
				case 0:
					DebugPrint("xhr请求已初始化");
					break;
				case 1:
					DebugPrint("xhr请求已打开");
					break;
				case 2:
					DebugPrint("xhr请求已发送");
					break;
				case 3:
					DebugPrint("xhr请求已响应");
					break;
				case 4:
					if (xhr.status == 200) {
						//DebugPrint("xhr请求成功：" + xhr.responseText);
						_apiManage.xhrSucess(sets, xhr);
					} else {
						DebugPrint("请求失败：" + xhr.readyState);
						sets.error("请求失败：" + xhr.readyState);
					}
					break;
				default:
					break;
			}
		};

		xhr.open(sets.type, sets.url);
		if (typeof(xhr) == 'undefined') {
			if (sets.error) {
				sets.error(_lang.xhrError);
			}
			return;
		}
		if (sets.header) {
			for (var i = 0; i < sets.header.length; i++) {
				xhr.setRequestHeader(sets.header[i][0], sets.header[i][1]);
			}
		}


		if (sets.type == 'POST') {
			var pd = '';
			if (sets.ContentType == 'application/x-www-form-urlencoded') {
				for (var k in sets.data) {
					pd += encodeURIComponent(k) + '=' + encodeURIComponent(sets.data[k]) + '&';
				}
				pd = pd.substr(0, pd.length - 1);
				//DebugPrint(pd);
				xhr.setRequestHeader('Content-Type', sets.ContentType);
				xhr.send(pd);
			} else {
				xhr.setRequestHeader('Content-Type', sets.ContentType);
				xhr.send(JSON.stringify(sets.data));
			}

		} else {
			xhr.send();
		}


		//xhr.open("GET", "http://www.baidu.com/");
		//xhr.send();
	},

	xhrSucess: function(sets, xhr) {
		var ret;
		try {
			ret = JSON.parse(xhr.responseText);
		} catch (e) {
			DebugPrint("解析错误," + e.message);
			//hui.toast("返回数据错误,"  +  xhr.responseText)
			sets.error("数据解析错误!");
		}

		if (!ret.hasOwnProperty("code")) {
			sets.success(ret);
			return;
		}
		if (ret.code && ret.code == "1") {
			sets.success(ret);
		} else {
			sets.error(ret.msg);
		}
	},




	makeUrl: function(url, param) {
		var returnStr = "";
		for (var key in param) {
			if (returnStr == "") {
				returnStr += "?";
			} else {
				returnStr += "&";
			}

			returnStr += key;
			returnStr += "=";

			//中文转码
			var keyChs = encodeURI(param[key]);
			//keyChs = encodeURI(keyChs);

			returnStr += keyChs;
		};
		return url + returnStr;
	}

}

//获取当前页面到登录页面的路径
function getLoginPath() {
	//var w = plus.webview.currentWebview();

}
